-- require"acc"
module(...,package.seeall)

local msgQuene = {}
local ready = false

function procMsg(socketClient)
	while #msgQuene>0 do
		local outMsg = table.remove(msgQuene,1)
		----------------------------------------------------------------------
		data = protoairdef.packAll(outMsg.t, outMsg.d)
        log.error("--zbb--linkout>>>:",string.toHex(data))
        ----------------------------------------------------------------------
        local result = socketClient:send(data)
		if outMsg.user then outMsg.user.cb(result,outMsg.user.para) end
		if not result then return end
	end
	return true
end

local function heartReport()
	log.error("--zbb--linkout heartReport:")
	protoairdef.heart()
end

local msgPosWillSendQuene = {}
function insertOutMsg(type,data,user)
	local sendImed = false
	if ready == false then
		if type == 0x0200 or type == 0x02 then
			print("--zbb--linkout.insertOutMsg1:",ready,type)
			table.insert(msgPosWillSendQuene,{t=type,d=data,user=user})
		elseif type == 0x0100 or type == 0x0102 then
			sendImed = true
		elseif type == 0x01 then
			sendImed = true
		end
	else
		sendImed = true
	end
	if sendImed then
		print("--zbb--linkout.insertOutMsg2:",ready,type)
		table.insert(msgQuene,{t=type,d=data,user=user})
		sys.timerStop(heartReport)
		sys.timerStart(heartReport,nvm.get("heart")*1000)
	end
end

function waitForSend()
	return #msgQuene > 0
end

function locReport(typ,user)
	protoairdef.loc()
end

function prelocReport()
	-- print("--zbb--prelocReport",shkcnt,gps.isFix())
	-- if shkcnt > 0 and gps.isFix() then
		locReport()	
	-- end
	local t
	if acc.getflag() then
		t = nvm.get("rptfreq")*1000
	else
		t = nvm.get("accOffRptfreq")*1000
	end
	sys.timerStart(prelocReport,t)
end

function sendUnSentPosMsg()
	local willSendQuene = {}
	print("--zbb--linkout.sendUnSentPosMsg:",#msgPosWillSendQuene)
	while #msgPosWillSendQuene>0 do
		local outMsg = table.remove(msgPosWillSendQuene,1)
		----------------------------------------------------------------------
		table.insert(willSendQuene,outMsg.d)
		-- print("--zbb--linkout.sendUnSentPosMsg2:",#willSendQuene)
		if #willSendQuene>=10 or #msgPosWillSendQuene<=0 then
		    protoairdef.locbatch(willSendQuene)
			while #willSendQuene>0 do
				local outMsg = table.remove(willSendQuene,1)
				if outMsg.user then outMsg.user.cb(true,outMsg.user.para) end
			end
		end
        ----------------------------------------------------------------------
		-- if outMsg.user then outMsg.user.cb(true,outMsg.user.para) end
		-- if not result then return end
	end
end

function initSuc()
	-- prelocReport()
	-- prelocReport()
	-- prelocReport()
	-- prelocReport()
	ready = true
	sendUnSentPosMsg()
end

function init()
	protoairdef.init()
end



-- local devpararpting

-- function getdevpara(idx)
-- 	devpararpting = true
-- 	local s = split.getfdata(nvm.paraname,idx,2048)
-- 	log.info("linkout.getdevpara",idx,string.len(s))
-- 	if not s or s=="" then
-- 		devpararpting = false
-- 	end
-- 	return s
-- end

-- local function devpararptreq(result,idx)
-- 	log.info("linkout.devpararptreq",devpararpting,idx)
-- 	if not devpararpting or idx then
-- 		local s = getdevpara(idx or 1)
-- 		if s and s~="" then 
-- 			--insertOutMsg(enTopic("devpararpt"),protoair.pack(protoair.RPTPARA,pack.pack("bA",protoair.LOG,s)),1,{cb=devpararptreq,para=(idx or 1)+1})
-- 		end
-- 	end
-- end

local slen,ssub,schar = string.len,string.sub,string.char

function unInit()
	sys.timerStop(locReport)
	sys.timerStop(heartReport)
	while #msgQuene>0 do
		local outMsg = table.remove(msgQuene,1)
		-- if outMsg.user then outMsg.user.cb(false,outMsg.user.para) end
		table.insert(msgPosWillSendQuene,outMsg)
	end
	-- devpararpting = nil
	ready = false
end

local shkcnt = 0
local function shkind()
	shkcnt = shkcnt + 1
	return true
end

local firstgps
local function gpstateind(evt)
	print("--zbb--linkout.gpstateind",evt,firstgps,rtos.poweron_reason())
	if evt == "LOCATION_SUCCESS" and not firstgps then
		if rtos.poweron_reason() == rtos.POWERON_KEY or rtos.poweron_reason() == rtos.POWERON_CHARGER then
			locReport()
			firstgps = true
		else
			prelocReport()
		end
	end
	return true
end

local function devEvent(evt)
	prelocReport()
end
local function chgind(e,val)
	print("--zbb--linkout.chgind",e,val)
	if e == "CHARGER" and ready then
		prelocReport()
	end
	return true
end

local function accind(on)
	prelocReport()
	-- if on then
		
	-- else
		
	-- end
	return true
end


--sys.subscribe("DEVPARA_RPT_REQ",devpararptreq)
sys.subscribe("SHKCNT_VALIDSHK_IND",shkind)
sys.subscribe("GPS_STATE",gpstateind)
sys.subscribe("MY_DEV_EVENT",devEvent)
sys.subscribe("DEV_CHG_IND",chgind)
sys.subscribe("DEV_ACC_IND",accind)

protoairdef.setInitFunc(insertOutMsg,initSuc)
